#!/bin/bash

params=$@

[ $# -eq 0 ] && { echo "$0: Missing params" ; echo "Usage: $0 [all=r1.0.0] pb-gateway=r1.0.0 pb-server=dev1 ..." ; exit 1 ; }

fullpath=`pwd -P .`
cd $fullpath


echo "--------------------------------------------------"
echo "Ready to git checkout branch: $params"
echo "--------------------------------------------------"

for p in $params ; do
  set -- `echo $p | tr '=' ' '` 
  k=$1 
  v=$2 
  if [ "$k" == "all" ] ; then
    for f in */ ; do
      if [ -d "$f" ] ; then
      	echo "------------------------< $f >"
        cd $f
    		branch=`git rev-parse --abbrev-ref HEAD`
				if [ "$branch" != "master" ] ; then
					git checkout $v ;
				fi ;
    		cd $fullpath ;
      fi ;
    done ;
  elif [ -d "$k" ] ; then
    cd $k 
    git checkout $v
    cd $fullpath ;
  else
    echo "$k directory not exists!"
    exit 1 ;
  fi 
done ;
    







